﻿// <copyright file="EsriPolygon.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>Implementation of EsriPolygon class.</summary>

namespace BESASM.EMGAATS.Arc
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using BESASM.EMGAATS.BusinessClasses;
  using ESRI.ArcGIS.esriSystem;
  using ESRI.ArcGIS.Geometry;

  /// <summary>
  /// An implementation of IPolygon which wraps an ESRI Polygon object.
  /// </summary>
  public class EsriPolygon : BusinessClasses.IPolygon
  {
    private ESRI.ArcGIS.Geometry.IPolygon esriPolygon;

    /// <summary>
    /// Initializes a new instance of the <see cref="EsriPolygon"/> class.
    /// </summary>
    /// <param name="polygon">The ESRI polygon this class will wrap.</param>
    public EsriPolygon(ESRI.ArcGIS.Geometry.IPolygon polygon)
    {
      this.esriPolygon = polygon;
    }

    /// <summary>
    /// Gets the ESRI Polygon this object wraps.
    /// </summary>
    public ESRI.ArcGIS.Geometry.IPolygon Polygon
    {
      get
      {
        return this.esriPolygon;
      }
    }

    #region IPolygon Members

    /// <summary>
    /// Gets or sets the length of the polyline
    /// </summary>
    public double Area
    {
      get
      {
        return ((ESRI.ArcGIS.Geometry.IArea)this.esriPolygon).Area;
      }

      set
      {
        throw new NotSupportedException(string.Format("Cannot set area of {0} directly.", this.GetType().Name));
      }
    }

    /// <summary>
    /// Returns a value indicating if this polygon has the same coordinates as the given polygon.
    /// </summary>
    /// <param name="polygon">The polygon to compare to this one.</param>
    /// <returns>True if the polygons are the same, otherwise false.</returns>
    public bool IsEquivalentTo(BusinessClasses.IPolygon polygon)
    {
      if (polygon == null)
      {
        return false;
      }

      if (polygon is EsriPolygon)
      {
        IRelationalOperator otherPolygon = ((EsriPolygon)polygon).Polygon as IRelationalOperator;
        return otherPolygon.Equals(this.esriPolygon);
      }
      else
      {
        throw new NotSupportedException("Cannot compare polylines that are not EsriPolylines.");
      }
    }

    /// <summary>
    /// Creates a new polygon that is a copy of the current instance.
    /// </summary>
    /// <returns>A new object that is a copy of this instance.</returns>
    public object Clone()
    {
      IClone polygonClone = ((IClone)this.esriPolygon).Clone();
      return new EsriPolygon((ESRI.ArcGIS.Geometry.IPolygon)polygonClone);
    }

    #endregion
  }
}